package com.foodie.service;

import com.foodie.dao.DB;
import com.foodie.entity.Menu;
import com.foodie.entity.User;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MenuService {
    public void add(Menu menu) {
        DB.executeUpdate("insert into t_menu(VIEW,MENU_NAME, MENU_INTRODUCE, IMAGE, MENU_DETAILS, MENU_TYPE, USER_ID, USER_NAME)" +
                        "VALUES(?,?, ?, ?, ?,?,(select USER_ID from t_user where token = ?),(select NAME from t_user where token = ?));",
                menu.getVIEW(),
                menu.getMENU_NAME(),
                menu.getMENU_INTRODUCE(),
                menu.getIMAGE(),
                menu.getMENU_DETAILS(),
                menu.getMENU_TYPE(),
                menu.getTOKEN(),
                menu.getTOKEN());
    }
    public List<Menu> menuList() {
        List<Map<String, Object>> menuList = DB.executeQuery("select * from t_menu");
        List<Menu> menus = new ArrayList<>();
        if (menuList != null) {
            for (int i = 0; i < menuList.size(); i++) {
                Map<String, Object> sc = menuList.get(i);
                Menu menu = new Menu();
                menu.setID((Integer) sc.get("ID"));
                menu.setMENU_NAME((String) sc.get("MENU_NAME"));
                menu.setMENU_INTRODUCE((String) sc.get("MENU_INTRODUCE"));
                menu.setUSER_NAME((String) sc.get("USER_NAME"));
                menu.setIMAGE((String) sc.get("IMAGE"));
                menu.setVIEW((Integer) sc.get("VIEW"));
                menu.setMENU_DETAILS((String) sc.get("MENU_DETAILS"));
                menu.setCREATE_TIME((String) sc.get("CREATE_TIME"));
                menu.setUPDATE_TIME((String) sc.get("UPDATE_TIME"));
                menus.add(menu);
            }
        }
        return menus;
    }
    public boolean usToken(String account) {
        return !DB.executeQuery("select count(*) as count from t_menu where TOKEN = ?", account).get(0).get("count").toString().equals("0");
    }

}
